package com.hpin.mlxteacher.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.utils.DensityUtil;
import com.hpin.mlxteacher.widget.PreviewPhotoActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

import java.util.ArrayList;

/**
 * Created by Aaron on 15/7/30.
 */
public class ShowImageAdapter extends BaseAdapter {

    private Context ctx;
    private ArrayList<String> pics;
    private DisplayImageOptions options;

    public ShowImageAdapter(Context ctx, ArrayList<String> pics) {
        this.ctx = ctx;
        this.pics = pics;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.icon_show_default)
                .showImageForEmptyUri(R.drawable.icon_show_default)
                .showImageOnFail(R.drawable.icon_show_default)
                .resetViewBeforeLoading(true)
                .cacheOnDisk(true).cacheInMemory(true)
                .cacheOnDisc(true).imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)
                .displayer(new SimpleBitmapDisplayer()).build();

    }

    @Override
    public int getCount() {
        return pics.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(ctx, R.layout.item_pic, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //如果只有一张图片, 放大图片
        if (pics.size() == 1) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(DensityUtil.dip2px(ctx, 120), DensityUtil.dip2px(ctx, 120));
            viewHolder.iv_image.setLayoutParams(params);
        } else {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(DensityUtil.dip2px(ctx, 85), DensityUtil.dip2px(ctx, 85));
            viewHolder.iv_image.setLayoutParams(params);
        }

        ImageLoader.getInstance().displayImage(pics.get(position), viewHolder.iv_image, options);
        final ArrayList<String> photoLists = pics;
        viewHolder.iv_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent mIntent = new Intent(ctx, PreviewPhotoActivity.class);
                mIntent.putExtra("position", position);
                mIntent.putStringArrayListExtra("preUrlPhotos", photoLists);
                ctx.startActivity(mIntent);
            }
        });

        return convertView;
    }


    private class ViewHolder {
        private ImageView iv_image;
    }

}
